/**
 * 
 */
package com.smarttest.common.beans.protolcol;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.smarttest.common.calculate.Mars2Wgs;
import com.smarttest.common.util.DateTimeUtils;



/**
 * GPS位置数据
 * @author Administrator
 *
 */
public class Location implements Serializable {
	private static final long serialVersionUID = 8422663310452675841L;
	
	/** 最小有效距离，大于这个距离的为有效移动 */
	public static final int VALID_DIST = 100;
	/** 离线时间阀值，超过这个时间没有收到数据则判为离线，单位：秒 */
	public static final long OFFLINE_DIFF = 1200;
	
	public static final int TYPE_GT01 = 1;
	public static final int TYPE_GT03 = 3;
	public static final int TYPE_GT06 = 6;
	public static final int TYPE_MOBILE = 20;
	public static final int TYPE_TC68 = 68;
	
	protected int mactype = TYPE_GT01;//设备类型，1：GT01 3:GT03	
	protected String imei;
	protected Date time;		// 定位时间
	protected Date createtime;		// 创建时间
	protected double lat;		// 纬度
	protected double lng;		// 经度
	protected double latw;		// 火星纬度
	protected double lngw;		// 火星经度
	protected int speed;		// 速度
	protected int course;		// 航向，360度，正北为0
	protected int distance;		// 和上一点的距离，厘米
	protected int voltage;		// 电压
	protected int gsm;			// GSM信号
	protected int gpsStatus = 1;// 定位状态
	protected int[] gpsSignal;	// 各卫星的信号
	int lbsCountry = 460;		// LBS信息，国家
	int lbsNetwork;				// 网络
	int lbsArea;				// 区域
	int lbsCellid;				// 基站
	
	// gps设备上传的, 将单位转化为米, 方便后面计算.
	private int totalMileage = -1;
	public int getTotalMileage() {
		return totalMileage;
	}

	public void setTotalMileage(int totalMileage) {
		this.totalMileage = totalMileage;
	}
	
	//位置附属信息
	private Map<Byte, Byte[]> additionInfo;
	
	protected int pyBlank = 10; // 两条有效数据最长时间间隔，单位：秒
	
	public Location() {}
	
	public Location(Date time, double lat, double latw, double lng, double lngw, int speed, int course, int distance) {
		this.time = time;
		this.lat = lat;
		this.latw = latw;
		this.lng = lng;
		this.lngw = lngw;
		this.speed = speed;
		this.course = course;
		this.distance = distance;
	}
	
	/**
	 * 手机定位
	 */
	public boolean isMobile() {
		return getMactype() == TYPE_MOBILE || isMobile(imei);
	}
	static public boolean isMobile(String imei) {
		return StringUtils.isNotBlank(imei) && StringUtils.startsWith(imei, "2001");
	}
	
	/**
	 * 当前处于有效移动状态（已排除漂移）
	 */
	public boolean isMoved() {
		return speed>3 && distance > VALID_DIST;
	}
	
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("Location:{");
		sb.append("imei:'").append(imei).append("',");
		sb.append("time:'").append(getTimeString()).append("',");
		sb.append("lnglat:").append(lng).append(",").append(lat).append(",");
		sb.append("speed:").append(speed).append(",");
		sb.append("distance:").append(distance).append(",");
		sb.append("course:").append(course).append(",");
		sb.append("voltage:").append(voltage).append(",");
		sb.append("pyBlank:").append(pyBlank);
		sb.append("}");
		return sb.toString();
	}
	
	public String getTimeString() {
		return DateTimeUtils.datetime2Text(time);
	}
	
	public String getImei() {
		return imei;
	}

	public Date getTime() {
		return time;
	}

	public double getLng() {
		return lng;
	}

	public double getLat() {
		return lat;
	}

	public int getSpeed() {
		return speed;
	}

	public int getCourse() {
		return course;
	}

	public double getLatw() {
		if(latw == 0 && lat != 0){
			double[] xy = Mars2Wgs.inverse(lng,lat);
			lngw = xy[0];
			latw = xy[1];
		}
		return latw;
	}

	public void setLatw(double latw) {
		this.latw = latw;
	}

	public double getLngw() {
		if(lngw == 0 && lng != 0){
			double[] xy = Mars2Wgs.inverse(lng,lat);
			lngw = xy[0];
			latw = xy[1];
		}
		return lngw;
	}

	public void setLngw(double lngw) {
		this.lngw = lngw;
	}

	public int getDistance() {
		return distance;
	}

	public void setDistance(int distance) {
		this.distance = distance;
	}

	public int getMactype() {
		return mactype;
	}

	public void setMactype(int mactype) {
		this.mactype = mactype;
	}

	public int getVoltage() {
		return voltage;
	}

	public int getGsm() {
		return gsm;
	}

	public int getGpsStatus() {
		return gpsStatus;
	}

	public int[] getGpsSignal() {
		return gpsSignal;
	}

	public void setImei(String imei) {
		this.imei = imei;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

	public void setTime(Date time) {
		this.time = time;
	}

	public void setLat(double lat) {
		this.lat = lat;
	}

	public void setLng(double lng) {
		this.lng = lng;
	}

	public void setCourse(int course) {
		this.course = course;
	}

	public void setVoltage(int voltage) {
		this.voltage = voltage;
	}

	public void setGsm(int gsm) {
		this.gsm = gsm;
	}

	public void setGpsStatus(int gpsStatus) {
		this.gpsStatus = gpsStatus;
	}

	public void setGpsSignal(int[] gpsSignal) {
		this.gpsSignal = gpsSignal;
	}

	public int getPyBlank() {
		return pyBlank;
	}

	public void setPyBlank(int pyBlank) {
		this.pyBlank = pyBlank;
	}

	public int getLbsCountry() {
		return lbsCountry;
	}

	public int getLbsNetwork() {
		return lbsNetwork;
	}

	public int getLbsArea() {
		return lbsArea;
	}

	public int getLbsCellid() {
		return lbsCellid;
	}

	public void setLbsCountry(int lbsCountry) {
		this.lbsCountry = lbsCountry;
	}

	public void setLbsNetwork(int lbsNetwork) {
		this.lbsNetwork = lbsNetwork;
	}

	public void setLbsArea(int lbsArea) {
		this.lbsArea = lbsArea;
	}

	public void setLbsCellid(int lbsCellid) {
		this.lbsCellid = lbsCellid;
	}

	/**
	 * @return the additionInfo
	 */
	public Map<Byte, Byte[]> getAdditionInfo() {
		return additionInfo;
	}

	/**
	 * @param additionInfo
	 *            the additionInfo to set
	 */
	public void setAdditionInfo(Map<Byte, Byte[]> additionInfo) {
		this.additionInfo = additionInfo;
	}

	public Date getCreatetime() {
		return createtime;
	}

	public void setCreatetime(Date createtime) {
		this.createtime = createtime;
	}
}

